﻿CREATE PROCEDURE [acms].[Post_GetTopLevelBySiteId]
	@SiteId int,
	@VersionTimeStamp datetime
AS
	if @VersionTimeStamp is null and exists (select null from [Post_View] WHERE SiteId = @SiteId and StartDate is null and EndDate is null and ParentId is null) begin
		SELECT *
		FROM [Post_View]
		WHERE SiteId = @SiteId
			and ParentId is null
			and StartDate is null
			and EndDate is null
	end
	else if @VersionTimeStamp is null begin
		SELECT *
		FROM [Post_View]
		WHERE SiteId = @SiteId
			and ParentId is null
			and StartDate is not null
			and EndDate is null
	end
	else begin
		SELECT *
		FROM [Post_View]
		WHERE SiteId = @SiteId
			and ParentId is null
			and StartDate <= @VersionTimeStamp
			and isNull(EndDate, @VersionTimeStamp) >= @VersionTimeStamp
	end

RETURN 0;